package com.example.xianzhi.controller.API;

import cn.hutool.json.JSONUtil;
import com.example.xianzhi.entity.TbAdd;
import com.example.xianzhi.service.AddService;
import com.example.xianzhi.service.impl.AddServiceImpl;
import org.lanqiao.mvc.entity.YockMvcAnnotation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@YockMvcAnnotation.Controller("/api_AddController")
public class Api_AddController {
    //地址的业务层
    private AddService add_sql = new AddServiceImpl();

    /**
     * 查询用户地址
     * @param request
     * @param response
     * @return
     */
    @YockMvcAnnotation.RequestMapping("/selectadd.do")
    @YockMvcAnnotation.ResponseBody
    public String selectgoodsorder(
            HttpServletRequest request,
            HttpServletResponse response,
            @YockMvcAnnotation.RequestParam(name = "user_id") int user_id

    ) {
        System.out.println(user_id);
        //告诉前端返回来的是一个json数据
        response.setContentType("application/json;charset=utf-8");
        // 保存地址的集合
        Map<String, Object> map = new HashMap<>();
        //查询地址数据
        List<TbAdd> tbAdds = add_sql.selectAdd(user_id);
        //允许的请求的域是*，代表所有
        response.setHeader("Access-Control-Allow-Origin","*");


        map.put("code",200);
        map.put("data",tbAdds);

        String jsonStr = JSONUtil.toJsonStr(map);


        return jsonStr;
    }

    /**
     * 添加地址
     * @param request
     * @param response
     * @throws Exception
     */
    @YockMvcAnnotation.RequestMapping("/insertadd.do")	//配置二级路径
    @YockMvcAnnotation.ResponseVoid
    public void addgoods(
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception{

        //允许的请求的域是*，代表所有
        response.setHeader("Access-Control-Allow-Origin","*");
//        TbGoods goods = new TbGoods();
//        goods.setGoods_name("平板");
//        goods.setGoods_picture("boy.jsp");
//        goods.setGoods_price(299);
//        goods.setGoods_old_price(99);
//        goods.setGoods_describe("Api15");
//        goods.setRelease_id(2);
//        goods.setType_id(1);
        int user_id  = Integer.parseInt(request.getParameter("users_id"));
        String name = request.getParameter("name");
        String num = request.getParameter("phone");
        String add = request.getParameter("add");


        TbAdd adds = new TbAdd();
        adds.setAdd_user_name(name);
        adds.setAdd_user_num(num);
        adds.setAdd_name(add);
        adds.setUser_id(user_id);

        add_sql.insertAdd(adds);

    }






}
